<?php

/**
 * Description of BaseModule
 *
 * @author David Dusek
 */
class ConfigModule extends CMS\AbstractModule {
    /**
     * Ziskani polozku admin menu.
     * @return array Asociativni pole s polozkami title, link, img.
     */
    public function hookAdminMenu() {
        return array("title" => "adminMenuTitle", 
                     "link" => ":Config:Config:default",
                     "img" => "btn_settings.png");
    }
    
    /**
     * Ziskani globalni konfigurace CMS.
     * @return mixed Pole s konfiguraci nebo FALSE kdyz se soubor s konfuguraci
     * nepodari otevrit. 
     */
    public function hookGetGlobalConfig() {
        $filename = MODULE_DIR . "/config.ini";
        if (!is_file($filename)) {
            return FALSE;
        }
        return parse_ini_file($filename);        
    }
    
    /**
     * Ziskani submenu pro modul.
     * @return array Asociativni pole obsahujici title, link a icon.
     */
    public function hookAdminSubmenu() {
        $submenu = array(
            0 => array(
                "title" => "settings",
                "link" => ":Config:Config:default",
                "icon" => "ui-icon-clipboard",
                "right" => "edit"
            ));
        return $submenu;
    }
    
    /**
     * Ziskani nazvu souboru ze slozky app/templates, ktere lze pouzit jako layout.
     * @param boolen $front Pokudje nastaven na true, tak jsou vraceny pouze
     * layouty pro frontend. Pokud je false, jsou vracenz layouty pro backhand.
     * @return array 
     */
    public function hookGetLayouts($front = FALSE) {
        $handle = opendir(APP_DIR . "/templates");
        $layouts = array();
        
        if ($handle) {
            while (false !== ($entry = readdir($handle))) {                
                $ext = substr($entry, strrpos($entry, "."), strlen($entry));
                        
                if ($front) {                    
                    $prefix = substr($entry, 0, 1);
                    if ($prefix == '@' && $ext == ".latte" && $entry[1] != '_') {                        
                        $layoutName = substr($entry, 1, strrpos($entry, ".") - 1);
                        $layouts[$layoutName] = $layoutName;
                    }
                } else {
                    $prefix = substr($entry, 0, 2);
                    if ($prefix == '@_' && $ext == ".latte") {
                        $layoutName = substr($entry, 2, strrpos($entry, ".") - 2);
                        $layouts[$layoutName] = $layoutName;
                    }
                }
            }
        }
        
        return $layouts;
    }
    
    /**
     * Ziskani souboru stylu ze slozky www/css.
     * @return array 
     */
    public function hookGetStylesheets($dir) {
        $handle = opendir(WWW_DIR . $dir);
        $stylesheets = array();
        
        if ($handle) {
            while (false !== ($entry = readdir($handle))) {                
                $ext = substr($entry, strrpos($entry, "."), strlen($entry));
                
                if ($ext == ".css") {                    
                    $stylesheets[$entry] = $entry;
                }
            }
        }
        
        return $stylesheets;
    }
    
    /**
     * Zapis do konfiguracniho souboru.
     * @param array $config Pole s konfiguraci.
     * @param boolean $hasSections Urcuje jestli ma konfigurace sekce.
     * @return boolean
     */
    function hookWriteConfig($config, $hasSections = FALSE) { 
        $content = ""; 
        if ($hasSections) { 
            foreach ($config as $key=>$elem) { 
                $content .= "[".$key."]\n"; 
                foreach ($elem as $key2=>$elem2) { 
                    if(is_array($elem2)) 
                    { 
                        for($i=0;$i<count($elem2);$i++) 
                        { 
                            $content .= $key2."[] = \"".$elem2[$i]."\"\n"; 
                        } 
                    } 
                    else if($elem2=="") $content .= $key2." = \n"; 
                    else $content .= $key2." = \"".$elem2."\"\n"; 
                } 
            } 
        } 
        else { 
            foreach ($config as $key=>$elem) { 
                if(is_array($elem)) 
                { 
                    for($i=0;$i<count($elem);$i++) 
                    { 
                        $content .= $key."[] = \"".$elem[$i]."\"\n"; 
                    } 
                } 
                else if($elem=="") $content .= $key." = \n"; 
                else $content .= $key." = \"".$elem."\"\n"; 
            } 
        } 

        if (!$handle = fopen(MODULE_DIR . "/config.ini", 'w')) { 
            return false; 
        } 
        if (!fwrite($handle, $content)) { 
            return false; 
        } 
        fclose($handle); 
        return true; 
    }
    
    public function hookUserGetAcl() {
        $acl = array(
            "Config" => array(
                "name" => "config_aclTitle",
                "operations" => array(
                    "edit" => "config_aclEdit"
                ),
            )
        );
        
        return $acl;
    }
}
